#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：BK_Knowledge_Graph 
@File    ：test1.py
@IDE     ：PyCharm 
@Author  ：HongYuan Guo
@Date    ：2023/4/4 14:09 
'''

import matplotlib.pyplot as plt
import numpy as np# 创建一个包含缺失值的数据框
import pandas as pd# 读取csv文件
def summary(df):
    data = df# 数据摘要
    summary = data.describe()# 输出摘要
    print(summary)

def table1(df):
    df.set_index(['YearStart', 'LocationDesc'], inplace = True)
    count_df = df.groupby(['YearStart', 'LocationDesc']).size().unstack()
    count_df.plot(kind='line')
    plt.xlabel('YearStart')
    plt.ylabel('LocationDesc')
    plt.title('Line charts of different place names and quantities at different times')
    plt.legend(loc = 'upper left', bbox_to_anchor = (0, 1), ncol = 2, fontsize = 8)
    plt.show()

def fill_ave(filename:str,need_fill_head:list):
    df = pd.read_csv(filename) # 计算平均年龄和平均薪水

    for nfh in need_fill_head:
        if nfh == 'views':
            dfh = df[nfh]
            res = []
            for d in dfh:
                d = str(d).replace(',','')
                res.append(d)
            df[nfh] = pd.Series(res).fillna('NaN').astype('float')
            df[nfh].fillna(df[nfh].mean(), inplace = True)
        else:
            mean = df[nfh].mean()
            df[nfh].fillna(mean, inplace = True)
    return df

def table2(df):
    plt.scatter(df['IMDb-rating'], df['views'])
    plt.xlabel('IMDb-rating')
    plt.ylabel('views')
    plt.title('The relationship between a movie‘s box office and ratings')
    plt.legend(loc = 'upper left', bbox_to_anchor = (0, 1), ncol = 2, fontsize = 8)
    plt.show()


if __name__ == '__main__':
    filename = 'movies_dataset.csv'
    df = fill_ave(filename,['IMDb-rating','views'])
    summary(df)
    table2(df)